package com.hb.software.attendance.impl;


import com.hb.software.attendance.AttendanceService;
import com.hb.software.attendance.dao.AttendanceDao;
import com.hb.software.entity.attendance.Attendance;


import com.hb.software.entity.attendance.AttendanceMessage;
import com.hb.software.service.impl.BaseServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;


@Service
@Transactional
public class AttendanceServiceImpl extends BaseServiceImpl<AttendanceMessage, Long> implements AttendanceService {

    private static final Logger logger = LogManager.getLogger(AttendanceServiceImpl.class.getName());


    private AttendanceDao attendanceDao;

    @Autowired
    public void setAttendanceDAO(AttendanceDao attendanceDao) {
        this.attendanceDao = attendanceDao;
        this.dao = this.attendanceDao;
    }


    @Override
    public List findByName(String name) {
        List list = attendanceDao.findByName(name);
        return list;
    }

    @Override
    public List findByData(String data) {
        return attendanceDao.findByData(data);
    }

    @Override
    public List findByTime(String time) { return attendanceDao.findByTime(time); }


    @Override
    public String createExcel(List<AttendanceMessage> list, String path) {

        // 创建一个Excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建一个工作表
        HSSFSheet sheet = workbook.createSheet("考勤表一");
        // 添加表头行
        HSSFRow hssfRow = sheet.createRow(0);
        // 设置单元格格式居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
//    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // 添加表头内容
        HSSFCell headCell = hssfRow.createCell(0);
        headCell.setCellValue("编号");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(1);
        headCell.setCellValue("姓名");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(2);
        headCell.setCellValue("签到日期");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(3);
        headCell.setCellValue("签到时间");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(4);
        headCell.setCellValue("签到状态");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(5);
        headCell.setCellValue("审核");
        headCell.setCellStyle(cellStyle);


        // 添加数据内容
        for (int i = 0; i < list.size(); i++) {
            hssfRow = sheet.createRow((int) i + 1);
            AttendanceMessage attendance = list.get(i);

            // 创建单元格，并设置值
            HSSFCell cell = hssfRow.createCell(0);
            cell.setCellValue(attendance.getId());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(1);
            cell.setCellValue(attendance.getName());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(2);
            cell.setCellValue(attendance.getSignInDate());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(3);
            cell.setCellValue(attendance.getSignInTime());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(4);
            cell.setCellValue(attendance.getStatus());
            cell.setCellStyle(cellStyle);


        }

        // 保存Excel文件
        try {
            OutputStream outputStream = new FileOutputStream(path);
            workbook.write(outputStream);
            outputStream.close();
        } catch (Exception e) {
            return "error";
        }
        return "success";
    }

}
